#!/bin/bash

if [ $# -lt 3 ] 
then
    echo"";
    echo "ERREUR: headergen prend en argument le nom de la structure sous differentes formes puis le type et le nom de chaque champ separé par un ':'";
    echo "Exemple: headergen struct STRUCT Struct :type1:nom1 unsigned:type2:nom2 :type3:nom3";
    echo "";    
    exit 2;
fi

echo "/**">>$1.h
echo " * @file $1.h">>$1.h
echo " * @brief Fichier d'entête du module $1.">>$1.h
echo " *">>$1.h
echo " * @author Delory Rachid, Keita Mamadou, Bernard Florian">>$1.h
echo " */">>$1.h
echo "#ifndef H_$2_H">>$1.h
echo "#define H_$2_H">>$1.h
echo "">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/*                   Librairies de $1                                    */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "">>$1.h
echo "#include <stdlib.h>">>$1.h
echo "#include <assert.h>">>$1.h
echo "#include <stdio.h>">>$1.h
echo "">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/*                        Définitions de types                           */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "">>$1.h
echo "/**">>$1.h
echo " * @struct s$3">>$1.h
echo " * @brief Structure utilisée pour la gestion d'un $1.">>$1.h
echo " *">>$1.h
echo " * Cette structure est utilisée pour gérer un $1.">>$1.h
echo " Les champs de cette structures doivent être modifiés ou lus">>$1.h
echo " par les accesseurs et mutateurs.">>$1.h
echo " */">>$1.h
echo "">>$1.h
echo "struct s$3 {">>$1.h
echo "">>$1.h
for I in "$@";do
    if [[ ! ("$I" = "$1" || "$I" = "$2" || "$I" = "$3") ]];then
	PTYPE=$(echo "$I" | cut  -sd":" -f1 ) 
	TYPE=$(echo "$I" | cut  -sd":" -f2 ) 
	NOM=$(echo "$I" | cut  -sd":" -f3 )
	if [ ! "$PTYPE" = "unsigned" ];then
	    if [ "$NOM" = "id" ];then
		echo  "        $TYPE $NOM ; /** Identifiant de "$1" */">>$1.h
	    else
		echo  "        $TYPE $NOM ;">>$1.h
	    fi
	else
	    if [ "$NOM" = "id" ];then
		echo  "        $PTYPE $TYPE $NOM ; /** Identifiant de "$1" */">>$1.h
	    else
		echo  "        $PTYPE $TYPE $NOM ;">>$1.h
	    fi
	fi
    fi
done
echo "">>$1.h
echo "} ;">>$1.h
echo "">>$1.h
echo "/**">>$1.h
echo " * @typedef $3">>$1.h
echo " * @brief Redéfinit la structure s$3.">>$1.h
echo " */">>$1.h
echo "typedef struct s$3 $3 ;">>$1.h
echo "">>$1.h
echo "">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/*             Fonctions relatives aux regroupement                      */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "">>$1.h
echo "">>$1.h
echo "/* ********************************************************************* */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/*           Accesseurs et Mutateurs de la structure Image               */">>$1.h
echo "/*                                                                       */">>$1.h
echo "/* ********************************************************************* */">>$1.h

for I in "$@";do
    if [[ ! ("$I" = "$1" || "$I" = "$2" || "$I" = "$3") ]];then
	PTYPE=$(echo "$I" | cut  -sd":" -f1 -s) 
	TYPE=$(echo "$I" | cut  -sd":" -f2 -s) 
	NOM=$(echo "$I" | cut  -sd":" -f3 -s)
	if [ "$PTYPE" = "unsigned" ];then
	    echo "">>$1.h
	    echo "">>$1.h
	    echo  "/**">>$1.h
	    echo " * @fn "$PTYPE" "$TYPE" get"$NOM"(const "$3" *"$1")">>$1.h
	    echo " * @brief">>$1.h
	    echo " *">>$1.h
	    echo " * @param">>$1.h 
	    echo " * @return">>$1.h
	    echo " */">>$1.h
	    echo ""$PTYPE" "$TYPE" get"$NOM"(const "$3" *"$1") ;">>$1.h
	else
	    echo "">>$1.h
	    echo "">>$1.h
	    echo  "/**">>$1.h
	    echo " * @fn "$TYPE" get"$NOM"(const "$3" *"$1")">>$1.h
	    echo " * @brief">>$1.h
	    echo " *">>$1.h
	    echo " * @param">>$1.h 
	    echo " * @return">>$1.h
	    echo " */">>$1.h
	    echo ""$TYPE" get"$NOM"(const "$3" *"$1") ;">>$1.h
	fi
	if [ "$PTYPE" = "unsigned" ];then
	    echo "">>$1.h
	    echo "">>$1.h
	    echo  "/**">>$1.h
	    echo " * @fn void set"$NOM"("$3" *"$1", const "$PTYPE" "$TYPE" "$NOM")">>$1.h
	    echo " * @brief">>$1.h
	    echo " *">>$1.h
	    echo " * @param [in, out]">>$1.h 
	    echo " * @return">>$1.h
	    echo " */">>$1.h
	    echo "void set"$NOM"("$3" *"$1", const "$PTYPE" "$TYPE" "$NOM")">>$1.h
	else
	    echo "">>$1.h
	    echo "">>$1.h
	    echo  "/**">>$1.h
	    echo " * @fn void set"$NOM"("$3" *"$1", const "$TYPE" "$NOM")">>$1.h
	    echo " * @brief">>$1.h
	    echo " *">>$1.h
	    echo " * @param [in, out]">>$1.h 
	    echo " * @return">>$1.h
	    echo " */">>$1.h
	    echo "void set"$NOM"("$3" *"$1", const "$PTYPE" "$TYPE" "$NOM")">>$1.h
	fi
    fi
done



